package com.bjbn.controller;

import com.bjbn.dto.SmsRequestDto;
import com.bjbn.dto.SmsResponseDto;
import com.bjbn.service.YxhtSmsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;

/**
 * 短信发送控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/sms")
@RequiredArgsConstructor
@Validated
@Tag(name = "短信发送", description = "提供短信发送相关接口")
public class SmsController {
    
    private final YxhtSmsService yxhtSmsService;
    
    /**
     * 发送短信（通用接口）
     */
    @Operation(summary = "发送短信", description = "通用短信发送接口，支持普通短信和模板短信")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "发送成功",
                    content = @Content(schema = @Schema(implementation = SmsResponseDto.class))),
            @ApiResponse(responseCode = "400", description = "参数错误",
                    content = @Content(schema = @Schema(implementation = SmsResponseDto.class))),
            @ApiResponse(responseCode = "500", description = "系统错误",
                    content = @Content(schema = @Schema(implementation = SmsResponseDto.class)))
    })
    @PostMapping("/send")
    public SmsResponseDto sendSms(@Valid @RequestBody SmsRequestDto request) {
        log.info("收到短信发送请求：{}", request);
        return yxhtSmsService.sendSms(request);
    }
    
    /**
     * 发送普通短信
     */
    @Operation(summary = "发送普通短信", description = "发送普通文本短信")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "发送成功",
                    content = @Content(schema = @Schema(implementation = SmsResponseDto.class))),
            @ApiResponse(responseCode = "400", description = "参数错误",
                    content = @Content(schema = @Schema(implementation = SmsResponseDto.class))),
            @ApiResponse(responseCode = "500", description = "系统错误",
                    content = @Content(schema = @Schema(implementation = SmsResponseDto.class)))
    })
    @PostMapping("/send/normal")
    public SmsResponseDto sendNormalSms(
            @Parameter(description = "手机号，多个用逗号分隔", required = true, example = "13800138000,13800138001")
            @RequestParam @NotBlank(message = "手机号不能为空") 
            @Pattern(regexp = "^1[3-9]\\d{9}(,1[3-9]\\d{9})*$", message = "手机号格式不正确") String mobiles,
            @Parameter(description = "短信内容", required = true, example = "测试短信")
            @RequestParam @NotBlank(message = "短信内容不能为空") String content,
            @Parameter(description = "短信名称", example = "测试短信")
            @RequestParam(required = false) String name) {
        
        log.info("收到普通短信发送请求，手机号：{}，内容：{}", mobiles, content);
        return yxhtSmsService.sendNormalSms(mobiles, content, name);
    }
    
    /**
     * 发送模板短信
     */
    @Operation(summary = "发送模板短信", description = "发送模板短信，需要先在易信华泰平台配置模板")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "发送成功",
                    content = @Content(schema = @Schema(implementation = SmsResponseDto.class))),
            @ApiResponse(responseCode = "400", description = "参数错误",
                    content = @Content(schema = @Schema(implementation = SmsResponseDto.class))),
            @ApiResponse(responseCode = "500", description = "系统错误",
                    content = @Content(schema = @Schema(implementation = SmsResponseDto.class)))
    })
    @PostMapping("/send/template")
    public SmsResponseDto sendTemplateSms(
            @Parameter(description = "手机号，多个用逗号分隔", required = true, example = "13800138000,13800138001")
            @RequestParam @NotBlank(message = "手机号不能为空") 
            @Pattern(regexp = "^1[3-9]\\d{9}(,1[3-9]\\d{9})*$", message = "手机号格式不正确") String mobiles,
            @Parameter(description = "模板ID", required = true, example = "TEMPLATE001")
            @RequestParam @NotBlank(message = "模板ID不能为空") String templateId,
            @Parameter(description = "模板参数数组", example = "param1=value1")
            @RequestParam(required = false) String[] params,
            @Parameter(description = "短信名称", example = "模板短信")
            @RequestParam(required = false) String name) {
        
        log.info("收到模板短信发送请求，手机号：{}，模板ID：{}", mobiles, templateId);
        return yxhtSmsService.sendTemplateSms(mobiles, templateId, params, name);
    }
    
    /**
     * 健康检查接口
     */
    @Operation(summary = "健康检查", description = "检查服务是否正常运行")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "服务正常", 
                    content = @Content(schema = @Schema(type = "string", example = "OK")))
    })
    @GetMapping("/health")
    public String health() {
        return "OK";
    }
} 